package br.edu.universo.si.projeto2.infojob.client.componentes;

import com.google.gwt.user.client.ui.FocusListener;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

/**
 * COMPONENTE - Campo para Data
 * @author Andrey A. de Oliveira
 * @since 21/09/2008
 */
public class DateBox extends TextBox
{
	
	public DateBox()
	{
		super();

		this.setWidth("100px");
		this.setTextAlignment(ALIGN_RIGHT);
		this.setMaxLength(10);
		
		this.addKeyboardListener(
			new KeyboardListener()
			{
				public void onKeyDown(Widget sender, char keyCode, int modifiers)
				{
					if (keyCode != '0' && keyCode != '1' && keyCode != '2' && keyCode != '3' && keyCode != '4' && 
						keyCode != '5' && keyCode != '6' && keyCode != '7' && keyCode != '8' && keyCode != '9' &&
						((int) keyCode) != KEY_BACKSPACE && ((int) keyCode) != KEY_TAB)
					{
						((TextBox) sender).cancelKey();
					}
				}

				public void onKeyPress(Widget sender, char keyCode, int modifiers)
				{
					if (keyCode != (char) KEY_BACKSPACE)
					{
						if (DateBox.this.getText().length() == 2
								|| DateBox.this.getText().length() == 5)
						{
							DateBox.this.setText(DateBox.this.getText() + "/");
						}
					}
				};

				// implementacao vazia...
				public void onKeyUp(Widget sender, char keyCode, int modifiers) { };
			}
		);

		// se o usuario inserir uma data invalida, o componente limpa a data quando perder o foco
		this.addFocusListener(
			new FocusListener()
			{
				public void onLostFocus(Widget sender)
				{
					if (DateBox.this.getText().length() != 10)
					{
						DateBox.this.setText("");
					}
				}

				public void onFocus(Widget sender) { };
			}
		);
	}
	
}